﻿#解析json文件,提取公司名称，职位，薪资等信息
import json
import csv
import os

import time
# 读取json数据
path=os.path.dirname(os.path.abspath(__file__))

# os.chdir()
# jsonpath  = path + "\\json\\51job_Qt_page1.json"

# 解析json数据，获取要存入csv文件的字段和值
records = []
n=1
for filename in os.listdir(path+"\\json"):
    if not filename.endswith(".json"):
        continue
    jsonpath = path + "\\json\\" + filename
    with open(jsonpath, "r", encoding='utf-8') as f:
        data = json.load(f)
    for item in data["resultbody"]["job"]["items"]:
        record = {}
        record["序号"] = n
        n = n+1
        record["招聘平台"] = "51job"
        keywordstr = item["property"]
        keywordjson = json.loads(keywordstr)
        record["搜索名称"] = keywordjson["keyword"]
        record["岗位名称"] = item["jobName"]
        record["公司名称"] = item["fullCompanyName"]
        record["工作地区"] = item["jobAreaString"]
        record["薪资范围"] = item["provideSalaryString"]
        record["工作年限"] = item["workYearString"]
        record["学历要求"] = item["degreeString"]
        record["岗位描述"] = item["jobDescribe"]
        record["岗位介绍"] = item["jobHref"]
        record["公司介绍"] = item["companyHref"]
        record["行业"] = item["industryType1Str"]
        record["类型"] = item["companyTypeString"]
        record["规模"] = item["companySizeString"]
        record["标签"] = str(item["jobTags"])
        record["更新时间"] = item["updateDateTime"]
        record["全职或兼职"] = item["termStr"]
        record["经度"] = item["lon"]
        record["纬度"] = item["lat"]
        records.append(record)

# 将数据存入csv文件
fieldnames = ["序号", "搜索名称", "招聘平台", "公司名称", "岗位名称", "薪资范围", "工作地区", "工作年限", "学历要求", "岗位描述", "岗位介绍", "行业", "类型", "规模",   "公司介绍", "标签", "更新时间", "全职或兼职", "经度", "纬度"]
csvpath = path + "\\job{}.csv".format(time.strftime("%Y%m%d_%H%M%S", time.localtime()))
with open(csvpath, "w", encoding='utf-8', newline="") as f:
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerows(records)